
/**
  ******************************************************************************
* @file				cmd.c
* @author			ou_teki
* @encoding			UTF-8 
* @date				2024-0420
  ******************************************************************************
  */

#ifdef __cplusplus
extern "C" {
#endif

#include "cmd.h"

CMD_ST cmd_help[CMD_MAX] = 
{
    {
        .cmd_str ="LED1 ON",
        .cmd_func = NULL,
    },
    {
        .cmd_str ="LED1 OFF",
        .cmd_func = NULL,
    },
    {
        .cmd_str ="LED1 TOGGLE",
        .cmd_func = NULL,
    },
};



uint8_t GetCmdIndex(char* cmd_str)
{
    uint8_t i;
    for(i = 0; i < CMD_MAX; i++)
    {
        if(strcmp(cmd_str, cmd_help[i].cmd_str) == 0)
        {
            if(cmd_help[i].cmd_func!= NULL)
            {
                cmd_help[i].cmd_func();
            }
            return i;
        }
    }
    return CMD_MAX;
}


void StringToLow(char *str)
{
    uint32_t i = 0;
    while('\0' != str[i])
    {
        if ((str[i]>='A') && (str[i]<='Z'))
        {
            str[i] += 32;
        }
        i++;
    }
}

void StringToUpper(char *str)
{
    uint32_t i = 0;
    while('\0' != str[i])
    {
        if ((str[i]>='a') && (str[i]<='z'))
        {
            str[i] -= 32;
        }
        i++;
    }
}

void StringSimplifySpaces(const char *in, char *out) 
{
    uint32_t len = strlen(in);
    uint32_t start = 0;
    uint32_t end = len - 1;

    // 去掉字符串开头的空格
    while (start <= end && isspace((unsigned char)in[start])) {
        start++;
    }

    // 去掉字符串结尾的空格
    while (end >= start && isspace((unsigned char)in[end])) {
        end--;
    }

    // 如果字符串全是空格，直接返回空字符串
    if (start > end) {
        *out = ' '; 
        out++;
        *out = '\0';
        return;
    }

    // 处理相邻多个空格转换为一个空格
    uint32_t space = 0; // 标记是否已经添加了空格

    for (uint32_t i = start; i <= end; i++) {
        if (!isspace((unsigned char)in[i])) {
            *out++ = in[i];
            space = 0;
        } else {
            if (!space) {
                *out++ = ' ';
                space = 1;
            }
        }
    }
    *out = '\0';
}


#ifdef __cplusplus
}
#endif
